import java.util.*;

class preorder {
  private static boolean DEBUG = true;
  
  public static class Node<T> {
    private T data = null;
    //private Node<T> parent = null;
    private Node<T> left = null;
    private Node<T> right = null;
  }
 
  public static class Tree<T> {
    private Node<T> root = null;

    /*public Tree(T rootData) {
      root = new Node<T>();
      root.data = rootData;
    }*/

    // linear search, since it is only a binary tree
    public Node<T> search(T targetData, Node<T> currentNode) {
      if (currentNode == null) {
        if (DEBUG) {
          System.out.println("OOOOPS: find a null Node~~~~");
        }
        return null;
      }
      if (currentNode.data.equals(targetData)) {
        return currentNode;
      } else {
        if (DEBUG) {
          System.out.println("Target Data: " + targetData + "; Current node data: " + currentNode.data);
        }
        Node<T> leftResult = search(targetData, currentNode.left);
        Node<T> rightResult = search(targetData, currentNode.right);
        if (leftResult != null) return leftResult;
        else return rightResult;
      }
    }

    public boolean insert(T parentData, T childData) {
      Node<T> childNode = new Node<T>();
      childNode.data = childData;

      // when a root doesn't exist, then insert as root
      if (this.root == null) {
        if (DEBUG) {
          System.out.println("The root is NULL!!!");
        }
        this.root = new Node<T>();
        this.root.data = parentData;
        root.left = childNode;
        return true;
      }

      // check whether a parent exist
      Node<T> parent = search(parentData, this.root);
      if (parent != null) {
        if (parent.left == null) {
          // insert as left child
          parent.left = new Node<T>();
          parent.left.data = childData;
        } else if (parent.right == null) {
          parent.right = new Node<T>();
          parent.right.data = childData;
        } else {
          System.out.println("ERROR: cannot insert a third child in a binary tree");
          return false;
        }
        return true;
      } else {
        if (DEBUG) {
          System.out.println("ERROR: parent is NULL!!!");
        }
      }
      return false;
    }
    
    public void inOrderTraversal() {
      inOrderNodePrintOut(this.root);
    }
    
    private void inOrderNodePrintOut(Node<T> currentNode) {
      if (currentNode == null) return;
      System.out.println(currentNode.data);
      inOrderNodePrintOut(currentNode.left);
      inOrderNodePrintOut(currentNode.right);
    }
  } 

  public static void run(Scanner scanner) {
    int treeEdges = Integer.parseInt(scanner.nextLine());
    String DEL = "  ";
    Tree<String> myTree = new Tree<String>();

    for (int i = 0; i < treeEdges; i++) {
      String [] currentEdge = scanner.nextLine().split(DEL);
      boolean test = myTree.insert(currentEdge[0], currentEdge[1]);
      if (DEBUG) {
        if (test) {
          System.out.println("Insert Success: Parent " + currentEdge[0] + "; Child " + currentEdge[1]);
        } else {
          System.out.println("Insert Failed: Parent " + currentEdge[0] + "; Child " + currentEdge[1]);
        }
      }
    }
    myTree.inOrderTraversal();
  }

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int cases = Integer.parseInt(scanner.nextLine());
    for(int i = 0; i < cases; i++) {
      run(scanner);
    }

    if (scanner != null) {
      scanner.close();
    }
  }
}